//
//  LKWorkOrderDetailContractDetailCell.m
//  YLHelper
//
//  Created by she1323 on 2018/4/26.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKWorkOrderDetailContractDetailCell.h"
#import "NSArray+Sudoku.h"
@interface LKWorkOrderDetailContractDetailCell()
@property (weak, nonatomic) IBOutlet UIView *imagesBgView;


@end


@implementation LKWorkOrderDetailContractDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(void)prepareForReuse{
    [super prepareForReuse];
}
-(void)setCustomerModel:(LKCustomerModel *)customerModel{
    _customerModel = customerModel;
    [self.selectShowArray removeAllObjects];
    _titleLabel.hidden = YES;
    [self.imagesBgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    NSArray  *imagesArray = customerModel.customerAttach;
    NSMutableArray *imgsArra = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < imagesArray.count; i++) {
        LKCustomerDetailAttachModel *model = imagesArray[i];
        UIImageView *imgaeView = [[UIImageView alloc] init];
        imgaeView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showBigPicture:)];
        [imgaeView addGestureRecognizer:tapGes];
        [self.selectShowArray addObject:[NSString stringWithFormat:@"%@%@",ServerlUrl,model.attachUrl]];
        imgaeView.tag = 1000 + i;
        [imgaeView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,model.attachUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
        [imgsArra addObject:imgaeView];
        [self.imagesBgView addSubview:imgaeView];
    }
    [imgsArra mas_distributeSudokuViewsWithFixedItemWidth:(WIDTH - 60)/3 fixedItemHeight: (WIDTH - 60)/3 * 65 / 95
                                         fixedLineSpacing:10 fixedInteritemSpacing:10
                                                warpCount:3
                                               topSpacing:10
                                            bottomSpacing:10 leadSpacing:10 tailSpacing:10];
}
-(void)setOrderModel:(LKOrderModel *)orderModel{
    _orderModel = orderModel;
    self.titleLabel.text = self.typeTitle;
    NSArray *imagesArray;
    if ([self.typeTitle isEqual:@"其他"]) {
        imagesArray = orderModel.orderAttachs0;
    }else if ([self.typeTitle isEqual:@"合同首页"]){
        imagesArray = orderModel.orderAttachs1;
    }else if ([self.typeTitle isEqual:@"合同详情"]){
        imagesArray = orderModel.orderAttachs2;
    }else{
        imagesArray = orderModel.orderAttachs3;
    }
    NSMutableArray *imgsArra = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < imagesArray.count; i++) {
        LKAttachModel *model = imagesArray[i];
        UIImageView *imgaeView = [[UIImageView alloc] init];
        imgaeView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showBigPicture:)];
        [imgaeView addGestureRecognizer:tapGes];
        [self.selectShowArray addObject:[NSString stringWithFormat:@"%@%@",ServerlUrl,model.attachUrl]];
        imgaeView.tag = 1000 + i;
        [imgaeView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,model.attachUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
        [imgsArra addObject:imgaeView];
        [self.imagesBgView addSubview:imgaeView];
    }
    [imgsArra mas_distributeSudokuViewsWithFixedItemWidth:(WIDTH - 60)/3 fixedItemHeight: (WIDTH - 60)/3 * 65 / 95
                                                            fixedLineSpacing:10 fixedInteritemSpacing:10
                                                                   warpCount:3
                                                                  topSpacing:10
                                                               bottomSpacing:10 leadSpacing:10 tailSpacing:10];
    
}
-(void)setAddOrderModel:(LKAddOrderModel *)addOrderModel{
    _addOrderModel = addOrderModel;
    self.titleLabel.text = self.typeTitle;
    NSArray  *imagesArray;
    if ([self.typeTitle isEqual:@"其他"]) {
        imagesArray = _addOrderModel.OrderAttachs0;
    }else if ([self.typeTitle isEqual:@"合同首页"]){
        imagesArray = _addOrderModel.OrderAttachs1;
    }else if ([self.typeTitle isEqual:@"合同详情"]){
        imagesArray = _addOrderModel.OrderAttachs2;
    }else{
        imagesArray = _addOrderModel.OrderAttachs3;
    }
    NSMutableArray *imgsArra = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < imagesArray.count; i++) {
        LKAddAttachModel *model = [LKAddAttachModel modelWithDictionary:imagesArray[i]];
        UIImageView *imgaeView = [[UIImageView alloc] init];
        imgaeView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showBigPicture:)];
        [imgaeView addGestureRecognizer:tapGes];
        [self.selectShowArray addObject:[NSString stringWithFormat:@"%@%@",ServerlUrl,model.AttachUrl]];
        imgaeView.tag = 1000 + i;
        [imgaeView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,model.AttachUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
        [imgsArra addObject:imgaeView];
        [self.imagesBgView addSubview:imgaeView];
    }
    [imgsArra mas_distributeSudokuViewsWithFixedItemWidth:(WIDTH - 60)/3 fixedItemHeight: (WIDTH - 60)/3 * 65 / 95
                                         fixedLineSpacing:10 fixedInteritemSpacing:10
                                                warpCount:3
                                               topSpacing:10
                                            bottomSpacing:10 leadSpacing:10 tailSpacing:10];
    
}

/**
 选中图片的点击手势

 @param tap 手势
 */
-(void)showBigPicture:(UITapGestureRecognizer *)tap{
    UIImageView *imageV = (UIImageView *)tap.view;

    [self showWithTouchIntag:imageV.tag - 1000 AndWithPicturesArray:self.selectShowArray AndSorceImageView:imageV];
}

#pragma mark 图片浏览器使用案例 (image brower use case)

#pragma mark 方式一：使用数组配置数据源
- (void)showWithTouchIntag:(NSInteger )indexPath AndWithPicturesArray:(NSArray *)picturesArray AndSorceImageView:(UIImageView *)currentImageV{
    
    //创建图片浏览器（注意：更多功能请看 YBImageBrowser.h 文件或者 github readme）
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.currentIndex = indexPath;
    
    //配置数据源（图片浏览器每一张图片对应一个 YBImageBrowserModel 实例）
    NSMutableArray *tempArr = [NSMutableArray array];
    [picturesArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        YBImageBrowserModel *model = [YBImageBrowserModel new];
        model.url = [NSURL URLWithString:obj];
        //        [model setImageWithFileName:obj fileType:@"png"];
        model.sourceImageView = currentImageV;
        [tempArr addObject:model];
    }];
    
    browser.dataArray = tempArr;
    
    //    YBImageBrowserFunctionModel *fm = [YBImageBrowserFunctionModel new];
    //    fm.name = @"分享";
    //    browser.fuctionDataArray = @[[YBImageBrowserFunctionModel functionModelForSavePictureToAlbum], fm];
    //展示
    [browser show];
}
-(NSMutableArray *)selectShowArray{
    if (!_selectShowArray) {
        _selectShowArray = [[NSMutableArray alloc] init];
    }
    return _selectShowArray;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
